package com.beemans.corekit.utils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.anythink.core.api.ATAdInfo;
import com.beemans.corekit.constant.ReportEventConstants;
import com.beemans.corekit.data.RemotePushEntity;
import com.beemans.corekit.data.RemotePushEvent;
import com.beemans.corekit.integration.admob.AdvertGsonUtils;
import com.beemans.corekit.integration.metrics.MetricsManager;
import com.beemans.corekit.integration.topon.ATAdInfoExt;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class ReportUtils {
    private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();

    /**
     * 上报广告事件
     */
    public static void reportAdvertAdInfoEvent(@NonNull String eventName, @Nullable ATAdInfo atAdInfo) {
//        EXECUTOR.submit(() -> {
//            FirebaseUtils.reportEvent(eventName);
//
//            final Map<String, Object> valParams = new HashMap<>();
//            try {
//                final int advertSum = AppCacheUtils.getAdvertEventSum(eventName);
//                valParams.put(ReportEventConstants.KEY_SUM, String.valueOf(advertSum));
//
//                if (atAdInfo != null) {
//                    try {
//                        final JSONObject atAdInfoObj = new JSONObject(String.valueOf(atAdInfo));
//                        valParams.put(ReportEventConstants.KEY_INFO, atAdInfoObj);
//                    } catch (Throwable ignored) {
//                    }
//                }
//            } catch (Throwable ignored) {
//            }
//            MetricsManager.getInstance().reportEvent(eventName, valParams, null);
//        });
    }

    /**
     * 上报广告事件
     */
    public static void reportAdvertAdInfoExtEvent(@NonNull String eventName, @Nullable ATAdInfoExt adInfoExt) {
//        EXECUTOR.submit(() -> {
//            FirebaseUtils.reportEvent(eventName);
//
//            final Map<String, Object> valParams = new HashMap<>();
//            try {
//                final int advertSum = AppCacheUtils.getAdvertEventSum(eventName);
//                valParams.put(ReportEventConstants.KEY_SUM, String.valueOf(advertSum));
//
//                if (adInfoExt != null) {
//                    try {
//                        final String json = AdvertGsonUtils.getInstance().toJson(adInfoExt);
//                        final JSONObject atAdInfoObj = new JSONObject(json);
//                        valParams.put(ReportEventConstants.KEY_INFO, atAdInfoObj);
//                    } catch (Throwable ignored) {
//                    }
//                }
//            } catch (Throwable ignored) {
//            }
//            MetricsManager.getInstance().reportEvent(eventName, valParams, null);
//        });
    }

    /**
     * 上报远程推送事件
     */
    public static void reportRemotePushEvent(
            @NonNull String eventName,
            @NonNull RemotePushEntity pushEntity
    ) {
        final Map<String, Object> valParams = new HashMap<>();
        try {
            final RemotePushEvent pushEvent = new RemotePushEvent();
            pushEvent.push_id = pushEntity.pushId;
            pushEvent.push_date = pushEntity.pushDate;
            pushEvent.country = pushEntity.country;
            pushEvent.locale = pushEntity.locale;

            final String json = AdvertGsonUtils.getInstance().toJson(pushEvent);
            final JSONObject object = new JSONObject(json);
            valParams.put(ReportEventConstants.REMOTE_PUSH_INFO, object);

            MetricsManager.getInstance().reportEvent(eventName, valParams, null);
        } catch (Throwable ignored) {
        }
    }
}